package cn.tang.tframe.sdm.aop.handler;

import cn.tang.tframe.common.utils.ToStringUtil;
import cn.tang.tframe.common.utils.lang.AnnotationUtils;
import cn.tang.tframe.sdm.aop.RecordLog;
import cn.tang.tframe.sdm.aop.SkipLog;
import org.aspectj.lang.ProceedingJoinPoint;
import org.slf4j.Logger;

import java.lang.reflect.Method;

/**
 * Created by tanghc on 17/1/18.
 */
public class AopLogFinalHandler extends FinalHandler {
	private String keyword;
	private Logger logger;

	public AopLogFinalHandler(String keyword, Logger logger){
		this.keyword = keyword;
		this.logger = logger;
	}
	@Override
	protected boolean doFinalHandler(ProceedingJoinPoint pjp, Object[] args, Method method, String className) {
		SkipLog skipLogAnnotation = AnnotationUtils.findAnnotation(method, SkipLog.class);
		if (null != skipLogAnnotation) {
			return true;
		} else {
			skipLogAnnotation = AnnotationUtils.findAnnotation(pjp.getTarget().getClass(), SkipLog.class);
			if (null != skipLogAnnotation) {
				return true;
			}
		}
		RecordLog recordLogAnnotation = AnnotationUtils.findAnnotation(method, RecordLog.class);
		if (null == recordLogAnnotation) {
			recordLogAnnotation = AnnotationUtils.findAnnotation(pjp.getTarget().getClass(), RecordLog.class);
			if (null == recordLogAnnotation) {
				return true;
			}
		}
		String argStr = ToStringUtil.reflectionToString(args, 50, 1024);
		logger.info("end {} {} {}: param={}, result={}, times={}ms",
				new Object[] { keyword, className, method.getName(), argStr, ToStringUtil.reflectionToString(result),
						elapse });
		return true;
	}
}
